/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.bean.InOutDetailBean;
import com.minierp.helper.EncCommonHelper;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class StockInOutListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"InOutList.xls\"");
		HSSFFont titltFont = workbook.createFont();
		titltFont.setFontHeightInPoints(16);
		titltFont.setBoldweight(700);
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		HSSFCellStyle titleCellStyle = workbook.createCellStyle();
		titleCellStyle.setFont(titltFont);
		titleCellStyle.setAlignment(2);
		titleCellStyle.setVerticalAlignment(1);
		HSSFCellStyle boldStyle = workbook.createCellStyle();
		boldStyle.setFont(boldFont);
		boldStyle.setBorderBottom(1);
		boldStyle.setBorderTop(1);
		boldStyle.setBorderLeft(1);
		boldStyle.setBorderRight(1);
		boldStyle.setAlignment(2);
		boldStyle.setVerticalAlignment(1);
		HSSFCellStyle norStyle = workbook.createCellStyle();
		norStyle.setBorderBottom(1);
		norStyle.setBorderTop(1);
		norStyle.setBorderLeft(1);
		norStyle.setBorderRight(1);
		boolean showCost = false;
		if (EncCommonHelper.checkPrivilege(request, "STOR_COST")) {
			showCost = true;
		}

		byte rowIndex = 0;
		HSSFSheet sheet = workbook.createSheet("list");
		String searchTime = model.get("searchTime").toString();
		String title = "收发存明细表";
		if (searchTime != null && !searchTime.equals("")) {
			title = title + "（" + searchTime + "）";
		}

		HSSFRow r = sheet.createRow(rowIndex);
		HSSFCell c = r.createCell(0);
		byte colNum = 17;

		for (int list = 1; list < colNum; ++list) {
			r.createCell((short) list);
		}

		c.setCellStyle(titleCellStyle);
		c.setCellValue(new HSSFRichTextString(title));
		sheet.addMergedRegion(new Region(rowIndex, 0, rowIndex,
				(short) (colNum - 1)));
		int arg22 = rowIndex + 1;
		if (showCost) {
			String arg23 = model.get("inCost").toString();
			String sf = model.get("outCost").toString();
			r = sheet.createRow(arg22);
			r.createCell(0)
					.setCellValue(
							new HSSFRichTextString("入库金额小计：" + arg23
									+ " 出库金额小计：" + sf));

			for (int billType = 1; billType < colNum; ++billType) {
				r.createCell((short) billType);
			}

			sheet.addMergedRegion(new Region(arg22, 0, arg22,
					(short) (colNum - 1)));
			++arg22;
		}

		r = sheet.createRow(arg22);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单号"));
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("日期"));
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("供需方"));
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("出入类型"));
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("备注"));
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("产品编号"));
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("品名规格"));
		c = r.createCell(7);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(8);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("入库"));
		c = r.createCell(9);
		c.setCellStyle(boldStyle);
		c = r.createCell(10);
		c.setCellStyle(boldStyle);
		c = r.createCell(11);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("出库"));
		c = r.createCell(12);
		c.setCellStyle(boldStyle);
		c = r.createCell(13);
		c.setCellStyle(boldStyle);
		c = r.createCell(14);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("结存"));
		c = r.createCell(15);
		c.setCellStyle(boldStyle);
		c = r.createCell(16);
		c.setCellStyle(boldStyle);
		++arg22;
		r = sheet.createRow(arg22);
		c = r.createCell(0);
		c.setCellStyle(boldStyle);
		c = r.createCell(1);
		c.setCellStyle(boldStyle);
		c = r.createCell(2);
		c.setCellStyle(boldStyle);
		c = r.createCell(3);
		c.setCellStyle(boldStyle);
		c = r.createCell(4);
		c.setCellStyle(boldStyle);
		c = r.createCell(5);
		c.setCellStyle(boldStyle);
		c = r.createCell(6);
		c.setCellStyle(boldStyle);
		c = r.createCell(7);
		c.setCellStyle(boldStyle);
		c = r.createCell(8);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(9);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单价（元）"));
		c = r.createCell(10);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("金额（元）"));
		c = r.createCell(11);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(12);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单价（元）"));
		c = r.createCell(13);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("金额（元）"));
		c = r.createCell(14);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(15);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("单价（元）"));
		c = r.createCell(16);
		c.setCellStyle(boldStyle);
		c.setCellValue(new HSSFRichTextString("金额（元）"));
		++arg22;
		sheet.addMergedRegion(new Region(arg22 - 2, 0, arg22 - 1, 0));
		sheet.addMergedRegion(new Region(arg22 - 2, 1, arg22 - 1, 1));
		sheet.addMergedRegion(new Region(arg22 - 2, 2, arg22 - 1, 2));
		sheet.addMergedRegion(new Region(arg22 - 2, 3, arg22 - 1, 3));
		sheet.addMergedRegion(new Region(arg22 - 2, 4, arg22 - 1, 4));
		sheet.addMergedRegion(new Region(arg22 - 2, 5, arg22 - 1, 5));
		sheet.addMergedRegion(new Region(arg22 - 2, 6, arg22 - 1, 6));
		sheet.addMergedRegion(new Region(arg22 - 2, 7, arg22 - 1, 7));
		sheet.addMergedRegion(new Region(arg22 - 2, 8, arg22 - 2, 10));
		sheet.addMergedRegion(new Region(arg22 - 2, 11, arg22 - 2, 13));
		sheet.addMergedRegion(new Region(arg22 - 2, 14, arg22 - 2, 16));
		List arg24 = (List) model.get("list");
		SimpleDateFormat arg25 = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < arg24.size(); ++os) {
			InOutDetailBean b = (InOutDetailBean) arg24.get(os);
			String arg26 = "";
			if (b.getBillType().equals("IN_BILL")) {
				arg26 = "入";
			} else if (b.getBillType().equals("PURC_BILL")
					&& b.getIoType().equals("IN")) {
				arg26 = "采购";
			} else if (b.getBillType().equals("PURC_BILL")
					&& b.getIoType().equals("OUT")) {
				arg26 = "退货";
			} else if (b.getBillType().equals("ALLO_BILL")
					&& b.getIoType().equals("IN")) {
				arg26 = "调入";
			} else if (b.getBillType().equals("ALLO_BILL")
					&& b.getIoType().equals("OUT")) {
				arg26 = "调出";
			}

			if (b.getBillType().equals("OUT_BILL")) {
				arg26 = "入";
			}

			r = sheet.createRow(os + arg22);
			c = r.createCell(0);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getBillCode()));
			c = r.createCell(1);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg25.format(b.getBillDate())));
			c = r.createCell(2);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getOtherName()));
			c = r.createCell(3);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(arg26));
			c = r.createCell(4);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getMark()));
			c = r.createCell(5);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getProductCode()));
			c = r.createCell(6);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getProductName()));
			c = r.createCell(7);
			c.setCellStyle(norStyle);
			c.setCellValue(new HSSFRichTextString(b.getProductUnit()));
			c = r.createCell(8);
			c.setCellStyle(norStyle);
			c.setCellValue(b.getInAmount().doubleValue());
			c = r.createCell(9);
			c.setCellStyle(norStyle);
			if (showCost) {
				c.setCellValue(b.getInUnitPrice().doubleValue());
			}

			c = r.createCell(10);
			c.setCellStyle(norStyle);
			if (showCost) {
				c.setCellValue(b.getInCost().doubleValue());
			}

			c = r.createCell(11);
			c.setCellStyle(norStyle);
			c.setCellValue(b.getOutAmount().doubleValue());
			c = r.createCell(12);
			c.setCellStyle(norStyle);
			if (showCost) {
				c.setCellValue(b.getOutUnitPrice().doubleValue());
			}

			c = r.createCell(13);
			c.setCellStyle(norStyle);
			if (showCost) {
				c.setCellValue(b.getOutCost().doubleValue());
			}

			c = r.createCell(14);
			c.setCellStyle(norStyle);
			c.setCellValue(b.getBalAmount().doubleValue());
			c = r.createCell(15);
			c.setCellStyle(norStyle);
			if (showCost) {
				c.setCellValue(b.getBalUnitPrice().doubleValue());
			}

			c = r.createCell(16);
			c.setCellStyle(norStyle);
			if (showCost) {
				c.setCellValue(b.getBalCost().doubleValue());
			}
		}

		ServletOutputStream arg27 = response.getOutputStream();
		workbook.write(arg27);
		arg27.flush();
		arg27.close();
	}
}